/*
package com.yeshine.opendata.utils;


import com.yeshine.opendata.entity.Data;
import com.yeshine.opendata.entity.DataSet;
import com.yeshine.opendata.entity.Org;
import com.yeshine.opendata.entity.User;
import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.*;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SearchEngine {


    private static final String  DATE_PATTEN="yyyy-MM-dd HH:mm:ss";


    private Logger logger=Logger.getLogger(this.getClass());
    private String indexPath="c:\\luceneindex\\dataindx";
    public String getIndexPath() {
        return indexPath;
    }

    public int setIndexPath(String indexPath) {
        this.indexPath = indexPath;
        return 1;
    }


    private Analyzer cnAnalyzer=new SmartChineseAnalyzer(Version.LUCENE_35);





    public String getString(Integer num){
        return  num==null?"":num+"";
    }

    public String getString(String str){
        return  StringUtils.hasLength(str)?str:"";
    }


    */
/**
     * 新增数据时同步
     * *//*

    @SuppressWarnings("deprecation")
    public int IndexData(Data data) {
        // TODO Auto-generated method stub
        File indexFile = new File(indexPath);
        Directory directory=null;
        try {
            directory = FSDirectory.open(indexFile);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            //log.logerror("open index file error:"+e1.getMessage());
            e1.printStackTrace();
        }
        // IndexWriter是用来操作（增、删、改）索引库的
        // true，表示每次都创建新的，有了就删掉再创建

        IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_35,
                cnAnalyzer);
        IndexWriter indexWriter;
        try {
            indexWriter = new IndexWriter(directory, conf);
            //File file = new File(filePath);
            Document document = new Document();

            document.add(new Field("ID",String.valueOf(data.getId()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("Creator_ID",String.valueOf(data.getCreater().getId()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("Title",getString(data.getTitle()),Store.YES,Index.ANALYZED));
            document.add(new Field("Description",getString(data.getDescription()),Store.YES,Index.ANALYZED));
            document.add(new Field("Tags",getString(data.getTags()),Store.YES,Index.ANALYZED));
            document.add(new Field("License",getString(data.getLicense()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("Orgnizeation_ID",String.valueOf(data.getOrg()!=null?data.getOrg().getId():""),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("Type",getString(data.getType()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("URL",getString(data.getUrl()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("Name",getString(data.getName()),Store.YES,Index.ANALYZED));
            document.add(new Field("Format",getString(data.getFormat()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("Visibility",getString(data.getVisibility()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("status",getString(data.getStatus()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("CurVersion",getString(data.getCurVersion()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("GUID",getString(data.getGUID()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("CreateTime",CommonUtil.dateFormat(data.getCreateTime(),DATE_PATTEN),Store.YES,Index.NOT_ANALYZED));

            document.add(new Field("datasetName",getString(data.getDataSet().getName()),Store.YES,Index.ANALYZED));//Dataset
            document.add(new Field("Create_Time",CommonUtil.dateFormat(data.getDataSet().getCreate_time(),DATE_PATTEN),Store.YES,Index.NOT_ANALYZED));//Dataset
            document.add(new Field("datasetDescription",getString(data.getDataSet().getDescription()),Store.YES,Index.ANALYZED));//Dataset
            document.add(new Field("Author_URL",getString(data.getDataSet().getAuthor_URL()),Store.YES,Index.NOT_ANALYZED));//Dataset
            document.add(new Field("InTime",CommonUtil.dateFormat(data.getDataSet().getInTime(),DATE_PATTEN),Store.YES,Index.NOT_ANALYZED));//Dataview
            document.add(new Field("datasetID",getString(data.getDataSet().getId()),Store.YES,Index.NOT_ANALYZED));//Dataset
            document.add(new Field("MaintainerID",String.valueOf(data.getDataSet().getMaintainer()!=null?data.getDataSet().getMaintainer().getId():""),Store.YES,Index.NOT_ANALYZED));//Dataset


            indexWriter.addDocument(document);
            indexWriter.optimize();
            indexWriter.close();
        } catch (CorruptIndexException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (LockObtainFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;
    }








    @SuppressWarnings("deprecation")
    //多条数据同步
    public int IndexDatas(List<Data> list)
    {
        File indexFile = new File(indexPath);
        Directory directory=null;
        try {
            directory = FSDirectory.open(indexFile);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            logger.error("open index file error:"+e1.getMessage());
            e1.printStackTrace();
            return 0;
        }
        IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_35,
                cnAnalyzer);
        IndexWriter indexWriter;
        try {
            indexWriter = new IndexWriter(directory, conf);
        } catch (CorruptIndexException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        } catch (LockObtainFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }
        for(int i=0;i< list.size();i++)
        {
            Data data= list.get(i);
            Document document = new Document();

            document.add(new Field("ID",String.valueOf(data.getId()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("Creator_ID",String.valueOf(data.getCreater().getId()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("Title",getString(data.getTitle()),Store.YES,Index.ANALYZED));
            document.add(new Field("Description",getString(data.getDescription()),Store.YES,Index.ANALYZED));
            document.add(new Field("Tags",getString(data.getTags()),Store.YES,Index.ANALYZED));
            document.add(new Field("License",getString(data.getLicense()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("Orgnizeation_ID",String.valueOf(data.getOrg()!=null?data.getOrg().getId():""),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("Type",getString(data.getType()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("URL",getString(data.getUrl()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("Name",getString(data.getName()),Store.YES,Index.ANALYZED));
            document.add(new Field("Format",getString(data.getFormat()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("Visibility",getString(data.getVisibility()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("status",getString(data.getStatus()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("CurVersion",getString(data.getCurVersion()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("GUID",getString(data.getGUID()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("CreateTime",CommonUtil.dateFormat(data.getCreateTime(),DATE_PATTEN),Store.YES,Index.NOT_ANALYZED));


            document.add(new Field("datasetName",getString(data.getDataSet().getName()),Store.YES,Index.ANALYZED));//Dataset
            document.add(new Field("Create_Time",CommonUtil.dateFormat(data.getDataSet().getCreate_time(),DATE_PATTEN),Store.YES,Index.NOT_ANALYZED));//Dataset
            document.add(new Field("datasetDescription",getString(data.getDataSet().getDescription()),Store.YES,Index.ANALYZED));//Dataset
            document.add(new Field("Author_URL",getString(data.getDataSet().getAuthor_URL()),Store.YES,Index.NOT_ANALYZED));//Dataset
            document.add(new Field("InTime",CommonUtil.dateFormat(data.getDataSet().getInTime(),DATE_PATTEN),Store.YES,Index.NOT_ANALYZED));//Dataview
            document.add(new Field("datasetID",getString(data.getDataSet().getId()),Store.YES,Index.NOT_ANALYZED));//Dataset
            document.add(new Field("MaintainerID",String.valueOf(data.getDataSet().getMaintainer()!=null?data.getDataSet().getMaintainer().getId():""),Store.YES,Index.NOT_ANALYZED));//Dataset


            try {
                indexWriter.addDocument(document);
            } catch (CorruptIndexException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try {
            indexWriter.optimize();
            indexWriter.close();
        } catch (CorruptIndexException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return list.size();
    }



    */
/**
     *查询   queryStr ??
     * *//*

    public List<Data> SearchData(String queryStr) {
        // TODO Auto-generated method stub
        List<Data> list=new ArrayList<Data>();
        String[] fields = { "Name", "Title" ,"Description","Tags"};
        QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_35,
                fields, cnAnalyzer);


        try {
            Query query = queryParser.parse(queryStr);

            //Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter("<font color=\"red\">", "</font>"), new QueryScorer(query));
            //highlighter.setTextFragmenter(new SimpleFragmenter(20));
            File indexFile = new File(indexPath);
            Directory directory = FSDirectory.open(indexFile);
            IndexReader indexReader = IndexReader.open(directory);
            IndexSearcher indexSearcher = new IndexSearcher(indexReader);
            Filter filter = null;
            ///filter.
            TopDocs topDocs = indexSearcher.search(query, filter, 10000);
            System.out.println("总共有【" + topDocs.totalHits + "】条匹配的结果");

            for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
                Data tmpdata=new Data();
                int docSn = scoreDoc.doc;// 文档内部编号
                Document document = indexSearcher.doc(docSn);// 根据文档编号取出相应的文档
                Data data=new Data();
                DataSet ds=new DataSet();
                System.out.println(document.get("ID"));
                data.setId(Integer.valueOf(document.get("ID")!=null&&!"null".equals(document.get("ID"))?document.get("ID"):"0"));
                data.setCreater(StringUtils.hasLength(document.get("Creator_ID")) ? new User(Integer.valueOf(document.get("Creator_ID"))) : null);
                data.setTitle(document.get("Title"));
                data.setDescription(document.get("Description"));
                data.setTags(document.get("Tags"));
                data.setLicense(document.get("License"));
                data.setOrg(StringUtils.hasLength(document.get("Orgnizeation_ID")+"")?new Org(Integer.valueOf(document.get("Orgnizeation_ID"))):null);
                data.setType(StringUtils.hasLength(document.get("Type"))?Integer.valueOf(document.get("Type")):null);
                data.setUrl(document.get("URL"));
                data.setName(document.get("Name"));
                data.setFormat(document.get("Format"));
                data.setVisibility(StringUtils.hasLength(document.get("Visibility"))?Integer.valueOf(document.get("Visibility")):null);
                data.setStatus(StringUtils.hasLength(document.get("status"))?Integer.valueOf(document.get("status")):null);
                data.setVersion(document.get("CurVersion").toString());
                data.setGUID(document.get("GUID"));
                data.setCreateTime(CommonUtil.dateParse(document.get("CreateTime").toString(), DATE_PATTEN));
                ds.setName(document.get("datasetName"));//Dataset
                ds.setCreate_time(CommonUtil.dateParse(document.get("Create_Time"), DATE_PATTEN));//Dataset
                ds.setDescription(document.get("datasetDescription"));//Dataset
                ds.setAuthor_URL(document.get("Author_URL"));//Dataset
                */
/*data.(Timestamp.valueOf(document.get("InTime")));//Dataviewdocument.get("InTime")*//*

                ds.setId(Integer.valueOf(document.get("datasetID")));//Dataset

                if (StringUtils.hasLength(document.get("MaintainerID"))){
                    ds.setMaintainer(new User(Integer.valueOf(document.get("MaintainerID"))));//Dataset
                }
                data.setDataSet(ds);
                list.add(data);
            }
            indexSearcher.close();
            indexReader.close();
        }
        catch (ParseException e) {
            // TODO Auto-generated catch block
            logger.error("parse search keywords error:"+e.getMessage());
            e.printStackTrace();
        }
        catch(IOException e)
        {
            logger.error("open index file error:"+e.getMessage());
            e.printStackTrace();
        }
		*/
/*catch (InvalidTokenOffsetsException e) {
			// TODO Auto-generated catch block
			log.logerror("token error");
			e.printStackTrace();
		}*//*

        return list;
    }



    */
/**
     * 删除 传入参数id
     * *//*

    public int deleteData(int id)
    {
        try {
            Directory dir = FSDirectory.open(new File(indexPath));
            IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_35,
                    cnAnalyzer);
            IndexWriter indexWriter = new IndexWriter(dir, conf);
            Term term = new Term("ID", String.valueOf(id));//删除field值为id的数据
            indexWriter.deleteDocuments(term);
            indexWriter.commit();
            indexWriter.close();
            logger.error("deleteData:data "+String.valueOf(id)+" has been deleted");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            logger.error("deleteData:open index file error");
            e.printStackTrace();
        }
        return 0;
    }


    @SuppressWarnings("deprecation")


    */
/**
     * 更新
     * *//*

    public int updateData(Data data)
    {
        try {
            Directory dir = FSDirectory.open(new File(indexPath));
            IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_35,
                    cnAnalyzer);
            IndexWriter indexWriter = new IndexWriter(dir, conf);
            Term term = new Term("ID", String.valueOf(data));//更新field值为id的数据
            Document document = new Document();
            document.add(new Field("ID",getString(data.getId()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("Creator_ID",getString(data.getCreater().getId()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("Title",getString(data.getTitle()),Store.YES,Index.ANALYZED));
            document.add(new Field("Description",getString(data.getDescription()),Store.YES,Index.ANALYZED));
            document.add(new Field("Tags",getString(data.getTags()),Store.YES,Index.ANALYZED));
            document.add(new Field("License",getString(data.getLicense()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("Orgnizeation_ID",String.valueOf(data.getOrg()!=null?data.getOrg().getId():""),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("Type",getString(data.getType()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("URL",getString(data.getUrl()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("Name",getString(data.getName()),Store.YES,Index.ANALYZED));
            document.add(new Field("Format",getString(data.getFormat()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("Visibility",getString(data.getVisibility()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("status",getString(data.getStatus()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("CurVersion",getString(data.getCurVersion()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("GUID",getString(data.getGUID()),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("CreateTime",CommonUtil.dateFormat(data.getCreateTime(),DATE_PATTEN),Store.YES,Index.NOT_ANALYZED));
            document.add(new Field("InTime",CommonUtil.dateFormat(data.getCreateTime(),DATE_PATTEN),Store.YES,Index.NOT_ANALYZED));//Dataview


            */
/*
            * DataSet
            * *//*

            document.add(new Field("datasetName",data.getDataSet().getName(),Store.YES,Index.ANALYZED));//Dataset
            document.add(new Field("Create_Time",CommonUtil.dateFormat(data.getDataSet().getCreate_time(),DATE_PATTEN),Store.YES,Index.NOT_ANALYZED));//Dataset
            document.add(new Field("datasetDescription",data.getDataSet().getDescription(),Store.YES,Index.ANALYZED));//Dataset
            document.add(new Field("Author_URL",data.getDataSet().getAuthor_URL(),Store.YES,Index.NOT_ANALYZED));//Dataset

*/
/*v           document.add(new Field("datasetID",String.valueOf(dv.getDs().getId()),Store.YES,Index.NOT_ANALYZED));//Dataset*//*

            document.add(new Field("MaintainerID",String.valueOf(data.getDataSet().getMaintainer()!=null?data.getDataSet().getMaintainer().getId():null),Store.YES,Index.NOT_ANALYZED));//Dataset
			*/
/*document.add(new Field("name", file.getName(), Store.YES,
            		Index.ANALYZED));
    		document.add(new Field("content", readFileContent(file), Store.YES,
            		Index.ANALYZED));
    		document.add(new Field("size", String.valueOf(file.length()),
            		Store.YES, Index.NOT_ANALYZED));// 不分词,但是有时需要索引,文件大小(int)转换成String
    		document.add(new Field("path", file.getAbsolutePath(), Store.YES,
            		Index.NOT_ANALYZED));*//*

            indexWriter.updateDocument(term, document);
            indexWriter.optimize();
            indexWriter.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            logger.error("updateData:open index file error:" + e.getMessage());
            e.printStackTrace();
        }
        return 0;
    }
}
*/
